package semanticAbstraction.wsmxAdapter;

import helper.stringHelper.StringHelper;
import helper.wsdlHelper.WSDLHelper;

import semanticAbstraction.wsmxAdapter.wsmlHelper.WSMOHelper;
import java.util.HashMap;

import org.omwg.ontology.Instance;
import org.omwg.ontology.Ontology;
import org.wsmo.common.IRI;
import org.wsmo.common.Namespace;
import org.wsmo.common.TopEntity;
import org.wsmo.common.exception.InvalidModelException;
import org.wsmo.wsml.ParserException;


/**
 * This class provides methods to directly interact with WSMX. The class is a proxy for the
 * WSMX Web Service Entrypoints.
 * 
 * @author Christian Stamber
 *
 */
public class WSMX_Entrypoints {
	
	
	public static String achieveGoal(String wsmlMessageGoal) throws Exception {
		HashMap response = WSDLHelper.invokeWebService(	WSMX_Constants.WSMX_ENTYPOINTS_WSDL,
														"WSMXEntryPoints",
														"achieveGoal",
														new String[]{wsmlMessageGoal});
		
		String resultStr;
		resultStr=StringHelper.getStringArray(response)[0];
		
		
		resultStr=applyOutputNamingConventions(resultStr);
		
		
		return resultStr;
	}
	
	
	public static String discoverWS(String semanticDescription) throws Exception {
		HashMap result;
		
		result=WSDLHelper.invokeWebService(	WSMX_Constants.WSMX_ENTYPOINTS_WSDL, 
											"WSMXEntryPoints", 
											//"achieveGoal",
											"discoverWebServices", 
											new String[]{semanticDescription});
		
		String resultStr;

		resultStr=StringHelper.getStringArray(result)[0];
		
		
		return resultStr;
	}
	
	public static void retrieveAllWebServices() throws Exception{
		HashMap result;
		
		result=WSDLHelper.invokeWebService(	WSMX_Constants.WSMX_ENTYPOINTS_WSDL, 
											"WSMXEntryPoints", 
											"retrieveWebServices", 
											new String[]{});
		
		String resultStr;
		
		System.out.println("--->"+result.size());
		
		resultStr=StringHelper.getStringArray(result)[0];
		System.out.println(resultStr);
	}
	
	public static void main(String[] args){
		try {
			
			String tmp="namespace {      q0 _\"http://org.ipsuper.composition.tp/CustomerIdentification#\",  \n    q1 _\"http://org.ipsuper.composition.tp/tpOntology#\" } \n ontology _\"http://www.wsmo.org/ontologies/resp8549907611152595317\" \n instance q0#CustomerIdentificationRequest memberOf q1#CustomerIdentificationRequest   \n   q1#customer hasValue q0#customerInstance  \n instance q0#customerInstance memberOf q1#TP_Customer    \n  q1#hasName hasValue \"Laurie\"";

			applyOutputNamingConventions(tmp);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
	public static String applyOutputNamingConventions(String message){
		try {
			Ontology ontology=WSMOHelper.parseOntology(message);
			
			String goalName="";
			String goalNamespace="";
			
			String namespaceStr;
			
			for (Namespace namespace : ontology.listNamespaces()) {
				System.out.println();
				namespaceStr=namespace.getIRI().toString();
				if(!namespaceStr.contains("tpOntology")&&!namespaceStr.contains("wsmostudio")){
					goalName=WSMOHelper.getRightmostPartOfNamespace(namespaceStr);
					goalNamespace=namespaceStr;
				}
			}
			
						
			IRI convIRI;
			Ontology convOntoloy;
			
			convIRI=WSMOHelper.wsmoFactory.createIRI(goalNamespace+goalName+"Output");
			
			convOntoloy=WSMOHelper.wsmoFactory.createOntology(convIRI);
			
			for (Instance instance : ontology.listInstances()) {
				convOntoloy.addInstance(instance);
			}
			
			String serializedConvOntoloy=WSMOHelper.serializeOntology(convOntoloy);
			
			System.out.println(serializedConvOntoloy);
			
			return serializedConvOntoloy;
			
		} catch (ParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidModelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "";
		
		
	}	
	
	
}
